Page({

  /**
   * 页面的初始数
   */
  data: {
    id1: 'back',
    id2: 'clear',
    id3: 'navgative',
    id4: '+',
    id5: '7',
    id6: '8',
    id7: '9',
    id8: '-',
    id9: '4',
    id10: '5',
    id11: '6',
    id12: '×',
    id13: '1',
    id14: '2',
    id15: '3',
    id16: '÷',
    id17: '0',
    id18: '.',
    id19: '%',
    id20: '=',
    screenData: '0',
    lastIsOperator: false,
    arr: []
  },
  //点击所有按钮的业务操作事件
  clickBtn: function(e) {
    var that = this
    var id = e.currentTarget.id
    if (id == this.data.id1) {
      that.clickBack(id) //退格键
    } else if (id == this.data.id2) {
      that.clickClear(id)
    } else if (id == this.data.id3) {
      that.clickNegative(id)
    }else if(id==this.data.id20){
      that.clickEqualitySign(id)
    } else {
      that.getScreenNum(id);
    }
  },
  //[1]点击任意键屏幕显示数据
  getScreenNum: function(id) {
    //如果打开计算器就开始单击操作符按钮
    if (id == this.data.id1 || id == this.data.id4 || id == this.data.id8 || id == this.data.id12 || id == this.data.id16 || id == this.data.id18 || id == this.data.id19 || id == this.data.id20) {
      if (this.data.lastIsOperator == true || this.data.screenData == 0) {
        return;
      }
    }
    //获取屏幕数据
    var sd = this.data.screenData;
    var data;
    if (sd == 0) {
      data = id;
    } else {
      data = sd + id
    }
    this.setData({
      screenData: data
    })
    this.data.arr.push(id)
    if (id == this.data.id4 || id == this.data.id8 || id == this.data.id12 || id == this.data.id16 || id == this.data.id18 || id == this.data.id19) {
      this.setData({
        lastIsOperator: true
      })
    } else {
      this.setData({
        lastIsOperator: false
      })
    }
  },
  //[2]删除和清空按钮逻辑的处理
  clickBack: function(id) {
    var data = this.data.screenData;

    if (data == 0) {
      return
    }
    data = data.substring(0, data.length - 1) //进行截取字符串
    if (data == "" || data == "-") {
      data = 0;
    }
    this.setData({
      screenData: data
    })
    this.data.arr.pop(); //去掉数组最后一位
  },
  //【3】清屏
  clickClear: function(id) {
    this.setData({
      screenData: 0
    })
    this.data.arr.length = 0
  },
  //[4]正负号按钮的业务处理
  clickNegative: function(id) {
    var data = this.data.screenData;
    if (data == 0) { //如果为零不做操作
      return
    }
    var firstWord = data.substring(0, 1);
    if (firstWord == "-") {
      data = data.substring(1, data.length)
      this.data.arr.shift();
    } else {
      data = '-' + data
      this.data.arr.unshift('-')
    }
    this.setData({
      screenData: data
    })
  },
  //[5]点击等号按钮业务逻辑
  clickEqualitySign: function(id) {
    var data = this.data.screenData //获取屏幕数据
    if (data == 0) { //进行数据判断，如果为零，不做操作
      return
    }
    var lastWord = data.substring(data.length - 1, data.length)
    if (isNaN(lastWord)) {
      return
    }
    var num = ""
    var lastOperator;
    var arr = this.data.arr
    var opptarr = []
    for (var i in arr) {
      if (isNaN(arr[i]) == false || arr[i] == this.data.id18 || arr[i] == this.data.id3) {
        num += arr[i]
      } else {
        lastOperator = arr[i];
        opptarr.push(num);
        opptarr.push(arr[i]);
        num = "";
      }
    }
    opptarr.push(Number(num));
    var result = Number(opptarr[0]) * 1.0;
    for (var i = 1; i < opptarr.length; i++) {
      if (isNaN(opptarr[i])) {
        if (opptarr[i] == this.data.id4){
          result += Number(opptarr[i+1]);
        } else if (opptarr[i] == this.data.id8){
          result -= Number(opptarr[i + 1]);
        } else if (opptarr[i] == this.data.id12) {
          result *= Number(opptarr[i + 1]);
        } else if (opptarr[i] == this.data.id16) {
          result /= Number(opptarr[i + 1]);
        } else if (opptarr[i] == this.data.id19) {
          result %= Number(opptarr[i + 1]);
        }
      }
    }
    this.data.arr.length=0;
    this.data.arr.push(result);
    this.setData({
      screenData:result+""
    })
  }
})